package com.example.study_thread.a_file;

import java.io.File;
import java.io.IOException;

public class Demo3File {
    public static void main(String[] args) {
//        file1();
        file2();
    }

    private static void file1() {
        File file1 = new File("1.txt");
        String absolutePath = file1.getAbsolutePath();
        System.out.println("absolutePath:" + absolutePath);

//        new File时写的是什么，getPath获得的就会是什么
        File file2 = new File("2.txt");
        String path = file2.getPath();
        System.out.println("path" + path);

        File file3 = new File("D:\\java_project\\study_thread\\src\\main\\java\\com\\example\\testFile\\test.jpg");
        System.out.println("file3.getName(): " + file3.getName());
        try {
            System.out.println("创建文件结果：" + file3.createNewFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("delete：" + file3.delete());

        File file4 = new File("D:\\java_project\\study_thread\\src\\main\\java\\com\\example\\testFile");
        System.out.println(file4.length());
        System.out.println("删除文件夹：" + file4.delete());

        File file = new File("D:\\java_project\\test_file\\haha\\heihei");
        System.out.println(file.mkdirs());
    }

    private static void file2() {
        File file = new File("D:\\java_project\\test_file\\haha\\heihei\\rain.txt");
        System.out.println(file.isDirectory()); // 判断是否为目录
        System.out.println(file.isFile());  // 判断是否为文件
        System.out.println(file.exists());

//        遍历文件对象
        File file1 = new File("D:\\java_project\\study_thread\\src\\main\\java\\com\\example\\study_thread");
        String[] list = file1.list();
        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i]);
        }

//        遍历指定的文件夹，返回的是file数组【推荐使用】
        System.out.println("================================");
        File[] files = file1.listFiles();
        for (File file2 : files) {
            System.out.println(file2);
        }
    }
}
